<template>
  <el-dialog title="新增部门" :visible="showdiolig" @close="btnclose">
    <el-form ref="deptForm" label-width="120px" :model="forData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="forData.name" style="width:80%" placeholder="请输入1-30个字" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="forData.code" style="width:80%" placeholder="请输入1-30个字" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="forData.manager" style="width:80%" placeholder="请选择" @focus="getSimplelist">
          <el-option v-for="value in people" :key="value.id" :lable="value.username" :value="value.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="forData.introduce" type="textarea" style="width:80%" :rows="3" placeholder="请输入1-300个字" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnclose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartment, getDepartDetail, updateDepartments } from '@/api/department'
import { getSimplelist } from '@/api/employees'
export default {
  props: {
    // 传入第一个值，用于显示显示或者隐藏
    showdiolig: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      //   因为这里要进行比较是否有重复名字，所以需要拿到部门结构名称
      const { depts } = await getDepartments()
      let isRepeat = false
      //  假如有id，那么就是编辑模式，需要将默认的验证去掉
      if (this.forData.id) {
        isRepeat = false
      } else {
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
        isRepeat ? callback(new Error(`该部门已经出现了${value}`)) : callback()
      }
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      // 因为在编辑状态中不需要校验
      let isRepeat = false
      if (this.forData.id) {
        isRepeat = depts.some(item => item.id !== this.forData.id && item.code === value && value)
      } else {
        isRepeat = depts.some(item => item.code === value && value)
      }
      isRepeat ? callback(new Error(`该部门编码已经出现过了，请重新输入`)) : callback()
    }
    return {
      forData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        //   做部门名称的校验
        name: [{ required: true, trigger: 'blur', message: '部门名称不能为空' },
          { min: 1, max: 30, trigger: 'blur', message: '部门名称要求1-30个字' },
          { trigger: 'blur', validator: checkNameRepeat }],
        code: [{ required: true, trigger: 'blur', message: '部门编码不能为空' },
          { min: 3, max: 30, trigger: 'blur', message: '部门编码要求1-30个字' },
          { trigger: 'blur', validator: checkCodeRepeat }],
        manager: [{ required: true, trigger: 'blur', message: '部门负责人不能为空' }],
        introduce: [{ required: true, trigger: 'blur', message: '部门介绍不能为空' },
          { min: 1, max: 300, trigger: 'blur', message: '部门介绍要求输入1-300个字' }]
      },
      people: []

    }
  },
  methods: {
    async getSimplelist() {
      const result = await getSimplelist()
      //   console.log(12323, result)
      this.people = result
      console.log(1323, this.people)
    },
    // 点击确定按钮
    btnOk() {
      this.$refs.deptForm.validate(async isOk => {
        if (isOk) {
          if (this.forData.id) {
            await updateDepartments(this.forData)
          } else {
            await addDepartment({ ...this.forData, pid: this.treeNode.id
            })
          }
          this.$emit('addDepts')
          this.btnclose()
        }
      })
    },
    btnclose() {
      this.forData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showdiolig', false)
    },
    async getDepartDetail(id) {
      this.forData = await getDepartDetail(id)
    }
  }
}
</script>

<style>

</style>
